var current = "";
var page = 1;
$(function () {
    var channel = getQueryString("channel");
    getBookDetail();
    getChapterInfo();
    getCatalog();
    getLikeData(channel);
    getPresentRecode('init');
    getPresentConfig();
    $(".back-wrapper").click(function () {
        location.href = "../index.html?channel=" + channel;
    });
    var flag = true;
    $(".extend").click(function () {
        if(flag){
            $(this).html("收缩");
            flag = false;
        }else {
            $(this).html("展开");
            flag = true;
        }
        $(".desc-detail p").toggleClass("limit");
    });
    $(".like-wrapper .more").click(function () {
        getLikeData(channel);
    });
    window.onscroll = function(){
        var t = document.documentElement.scrollTop || document.body.scrollTop;  //获取滚动的距离
        if(t > 250){
            $(".back-top-wrapper").css("opacity",1);
        }else{
            $(".back-top-wrapper").css("opacity",0);
        }
    };
    $(".back-top-wrapper").click(function () {
        window.scrollTo(0,0);//移动到顶部
    });

    $(".gift-btn-wrapper").click(function () {
        showPresentList();
    });

    $(".present").click(function () {
        showPresentList();
    });

    $(".select-present-top-mask").click(function (e) {
        $(".select-present-mask").hide();
    });

    $(".present-list").on("click","li",function () {
        $(this).addClass("selected").siblings("li").removeClass("selected");
        $(this).children(".border-line").show();
        $(this).siblings("li").children(".border-line").hide();
    });

    $(".more-present").click(function () {
        getPresentRecode();
    });

    $(".more-catalog").click(function () {
        location.href = "./catalog.html?id="+getQueryString("id")+"&channel="+getQueryString("channel") + "&current=" + current;
    });
});

function getBookDetail() {
    $.ajax({
        type:'get',
        url:BASE_URl + "book/profile?id=" + getQueryString("id"),
        success:function (data) {
            if(data.success){
                DrawBookDetail(data.data);
            }
        }
    });
}

function DrawBookDetail(data) {
    $(".book-detail-wrapper .left-wrapper img").attr("src",data.image);
    $(".book-name").html(data.name);
    $(".words").html((data.words/10000).toFixed(1) + "万");
    $(".status").html(data.status);
    $(".read-count").html(data.clicks);
    var html = "";
    for(var i = 0;i < data.tags.length;i++){
        html += `<li class="inline">${data.tags[i]}</li>`;
    }
    $(".cat-list").html(html);
    $(".desc-detail .limit").html(data.desc);
    if(data.scope.indexOf("fav") > -1){
        $(".added").show().siblings(".add-shelf").hide();
    }else{
        $(".added").hide().siblings(".add-shelf").show();
    }
    if(data.type == "whole" && data.scope.indexOf("buy") > -1){
        $(".sell-book .sell-price").html(data.sell_price);
        $(".sell-book .origin-price").html(data.original_price);
        $(".buy-whole-wrapper .sell-book").show();
    }
    if(data.type == "whole" && data.scope.indexOf("bought") > -1){
        $(".buy-whole-wrapper .has-bought").show();
    }
}

function buyBook() {
    $.ajax({
        type:'get',
        url:BASE_URl + "book/buy?id=" + getQueryString("id"),
        success:function (data) {
            if(data.success){
                Toast("购买成功");
                $(".buy-whole-wrapper .sell-book").hide();
                $(".buy-whole-wrapper .has-bought").show();
            }else{
                if (data.data.ercode == "U100004") {
                    Toast("书币余额不足");
                } else {
                    Toast(data.data.ercode);
                }
            }
        }
    });
}

function addShelf() {
    $.ajax({
        type:'get',
        url:BASE_URl + "book/fav?id=" + getQueryString("id"),
        success:function (data) {
            if(data.success){
                Toast("已加入书架");
                $(".added").show().siblings(".add-shelf").hide();
            }
        }
    });
}

function getChapterInfo() {
    $.ajax({
        type:'get',
        url:BASE_URl + "chap/info?id=" + getQueryString("id"),
        success:function (data) {
            if(data.success){
                if(data.data.current == undefined){
                    var html = `<div onclick="goRead('${data.data.first.id}')">
                                    <div class="icon-and-start">
                                        <i class="inline read-icon"></i><span class="inline start-read">开始阅读</span>
                                    </div>
                                </div>`;
                    $(".read-btn-wrapper").html(html);
                }else{
                    current = data.data.current.id;
                    var html = `<div onclick="goRead('${data.data.current.id}')">
                                    <div class="icon-and-start">
                                        <i class="inline read-icon"></i><span class="inline start-read">继续阅读</span>
                                    </div>
                                </div>`;
                    $(".read-btn-wrapper").html(html);
                }
                $(".catalog-wrapper .head .right-wrapper span").html(data.data.last.name);
            }
        }
    });
}

function goRead(id) {
    location.href = "./read.html?id="+getQueryString("id")+"&cid="+id+"&channel="+getQueryString("channel");
}

function getCatalog() {
    $.ajax({
        type:'get',
        url:BASE_URl + "chap/list?id=" + getQueryString("id") + "&page=1&limit=4&sort=0&no=0",
        success:function (data) {
            if(data.success){
                var html = "";
                for(var i = 0;i < data.data.length;i ++){
                    html += `<li onclick="goRead('${data.data[i].id }')">${data.data[i].name}</li>`;
                }
                $(".four-chapter ul").html(html);
            }
        }
    });
}

function getLikeData(channel) {
    $.ajax({
        type:'get',
        url:BASE_URl + "topic/gene?channel="+channel+"&size=3&format=normal",
        success:function (data) {
            if(data.success){
                DrawLike(data.data,channel);
            }
        }
    });
}

function DrawLike(likeData,channel) {
    var html = "";
    for (var i = 0;i < likeData.length;i ++){
        html += `<li class="inline" onclick="goDetail('${likeData[i].id}',${channel})">
                        <div class="like-cover"><img src="${likeData[i].image}" width="100%" height="100%"></div>
                        <p class="like-name">${likeData[i].name}</p>
                    </li>`;
    }
    $(".like-content ul").html(html);
}

function goDetail(id,channel) {
    location.href = "./detail.html?id=" + id + "&channel=" + channel;
}

function getPresentRecode(status) {
    $.ajax({
        type:'get',
        url:BASE_URl + "book/prop?id=" + getQueryString("id") + "&page=" + page + "&limit=5",
        success:function (data) {
            if(data.success){
                if(!data.data){
                    if(status == "init"){// 如果是初始化该页面，没有打赏记录时，不弹出提示
                        return;
                    }
                    Toast("没有更多了");
                }else{
                    page ++;
                    DrawPresentRecode(data.data);
                }
            }
        }
    });
}

function DrawPresentRecode(data) {
    var html = "";
    for(var i = 0;i < data.length;i ++){
        html += `
                <li class="border-1px">
                    <div class="head-img-wrapper inline"><img src="${data[i].avatar}" alt=""></div>
                    <div class="present-detail-wrapper inline">
                        <p class="nickname">${data[i].nickname}</p>
                        <p class="data-time">${data[i].pdate}</p>
                        <p class="present-detail">赠送<span class="shit-icon" style="background-image: url(${data[i].pimage})"></span><span class="present-item">${data[i].pname}</span>给作者</p>
                    </div>
                </li>
        `;
    }
    $(".present-content ul").append(html);
}

function showPresentList() {
    $(".select-present-mask").show();
}

function getPresentConfig() {
    $.ajax({
        type:'get',
        url:BASE_URl + "config/prop",
        success:function (data) {
            if(data.success){
                DrawPresentConfig(data.data);
            }
        }
    });
}

function DrawPresentConfig(data) {
    var html = "";
    for(var i = 0;i < data.prop.length;i ++){
        html += `
                <li class="border-1px-all" data-id="${data.prop[i].id}">
                    <div class="present-item">
                        <div class="present-img"><img src="${data.prop[i].image}" alt="" width="100%" height="100%"></div>
                        <div class="present-name tc">${data.prop[i].name}</div>
                        <div class="present-price tc">${data.prop[i].price}</div>
                    </div>
                    <div class="border-line"></div>
                </li>
        `;
    }
    $(".select-present-wrapper .present-list").html(html);
    $(".rest-coin span").html(data.coin);
}

function givePresent() {
    var present = $(".present-list li.selected");
    if(present.length == 0){
        Toast("请先选择您要打赏的礼物");
    }else{
        var propId = present[0].dataset.id;
        $.ajax({
            type:'get',
            url:BASE_URl + "user/prop/send?book=" + getQueryString("id") + "&prop=" + propId,
            success:function (data) {
                if(data.success){
                    $(".present-content ul li").remove();
                    page = 1;
                    Toast("打赏成功");
                    getPresentRecode();
                    $(".select-present-mask").hide();
                }else{
                    if (data.data.ercode == "U100004") {
                        Toast("书币余额不足");
                    } else {
                        Toast(data.data.ercode);
                    }
                }
            }
        });
    }
}
